# -*- coding: utf-8 -*-
# @File  : locustShiLi.py
# @Author: zbh
# @Date  : 2025/2/20_16:07


import os
import time
from json import JSONDecodeError

from locust import HttpUser, task, between,TaskSet

url = "http://192.168.99.99/acp-custody/rest/login/extend/userForLogin?token=null"
payload = 'loginName=ab&password=HEX-04eea7fe393feb4ef5b3b75468ace6f618c067488e9f90dcaba2da754f5e4493f014789a457b27649c0e39eff5788a312c308db9b94a0d725acad5bc0c14dbf2dbd1e6c9b8300cbe1a3d390666eadc92d6762a8a40e3ec0d9f06577e5379e4058f3f5e1be38a1c2c640989796b61&validType=0&privateKey=ACF40088C2EE020AAFBC396C8131AC6311F57C9608BF1800E9657AF1B6F9C646'
headers = {
  'Content-Type': 'application/x-www-form-urlencoded',
  'Cookie': 'JSESSIONID=0000dHiCmreU0dKYE_Iza1LC-Yb:-1; acp_cookie_name=6E586A70529D27BE63459AB5F76E676234F943CAD98BB64870604F0D'
}

payload2="id=22121910535990014435&params=%5B%5D&startStep=&steps=%5Bobject%20Object%5D"
# 定义一个任务类继承TaskSet类
class HelloWorld1(TaskSet):
    '''TaskSet或SequentialTaskSet，当类里面的任务请求有先后顺序时继承SequentialTaskSet类，没有先后顺序，
    可以使用继承TaskSet类'''
    # 定义一个任务
    @task(3)  # 是装饰器，声明此方法是一个任务，权重默认是1
    def hello_world(self):
        # 发送一个GET请求到"/"路径
        with self.client.get("http://192.168.99.99/acp-custody/rest/login/extend/findAllImgInfo?token=null&count=0&_=1740125931823", catch_response=True) as response:
            if response.status_code == 200:
                response.success()
        print("打开登录页面状态码33333333：", response.status_code)


class HelloWorld2(TaskSet):

    @task(1)  # 声明此方法是一个任务，权重为2
    def chaxun(self):
        url="http://192.168.99.87:8090/api/amc-datalink-server/data-view/query/http"
        parload2={"path":"/lifecycle","methodName":"GET","linkId":"d7c356e8a0084eb4a567422893c5875d","contentType":3,"queryParams":[{"code":"code","value":"ZQCS002137"},{"code":"current","value":1},{"code":"size","value":10}]}
        with self.client.post(url,data=payload2, headers=headers,catch_response=True) as response:
            if "操作成功" in response.text:
                response.success()
        print("查询状态码2222：",response.status_code)

class UserRun(HttpUser):
    host = "http://192.168.99.87:8090"
    tasks = [HelloWorld2]

# class UserRun3(HttpUser):
#     host = "http://192.168.99.99/80"
#     tasks = [HelloWorld1]



# 定义一个运行继承HttpUser类
class UserRun1(HttpUser):   # HttpUser：主要用于做http请求，基于reques进行了封装，定义session
    host = "http://192.168.99.99/80"
    # 声明执行的任务集是那个类
    tasks = [HelloWorld1]  # 用于指向用户行为，可以定义为列表或字典，列表类型时采用随机访问方式，为字典类型时可以自定义执行权重。权重越高，执行概率越大
    # 声明运行过程中的间隔时间,设置每用户执行事务等待时间。单位：秒
    wait_time = between(1, 5)  # 表示用户执行任务之间的等待时间在1到5秒之间随机取值

    # 每用户初始动作，作用等同于pytest，unittest的setup
    def on_start(self):
        with self.client.post(url,data=payload, headers=headers,catch_response=True) as response:
            if response.status_code != 200:  # 判断响应状态码是否为200
                response.failure("登录失败")
            elif response.elapsed.total_seconds() > 3:  # 判断响应时间是否大于3秒
                response.failure("Request took too long")
            elif "ab" not in response.text:  # 判断响应内容是否包含"ab"
                response.failure("Response does not contain 'ab'")
        print("登录状态码1111111111：", response.status_code)

    #  每用户结束动作，作用等同于pytest，unittest的teardown
    def on_stop(self):
        self.client.close()

# if __name__ == '__main__':
#     os.system("locust -f locustShiLi.py")
#
#











